package com.km.service.impl;

import com.dingtalk.chatbot.message.TextMessage;
import com.google.common.base.Strings;
import com.km.core.utils.OKHttp;
import com.km.dao.DingdingTokenDao;
import com.km.entity.DingdingMsgEntity;
import com.km.entity.DingdingTokenEntity;
import com.km.service.DingdingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>钉钉消息处理</p>
 * Created by zhezhiyong@163.com on 2017/6/19.
 */
@Service
public class DingdingServiceImpl implements DingdingService {

    @Resource
    private DingdingTokenDao dingdingTokenDao;

    @Override
    public void sendTextMsg(DingdingMsgEntity entity) {
        List<String> atMobiles = new ArrayList<>();
        if (!Strings.isNullOrEmpty(entity.getAt())) {
            atMobiles.addAll(Arrays.asList(entity.getAt().split(",")));
        }
        TextMessage message = new TextMessage(entity.getContent(), atMobiles, entity.getIsatall() == 1);
        DingdingTokenEntity dingdingTokenEntity = dingdingTokenDao.queryByFlag(entity.getFlag());
        OKHttp.post(dingdingTokenEntity.getTokenUrl(), message.toJsonString());
    }
}
